package site.wanjiahao.handler.client;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;
import site.wanjiahao.app.Client;
import site.wanjiahao.message.request.*;

import java.util.Arrays;
import java.util.Scanner;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;

@Slf4j
public class MyChannelInboundCycleHandlerAdapter extends ChannelInboundHandlerAdapter {

    private final CountDownLatch AWAIT_SIGNAL;

    private final AtomicBoolean SUCCESS;

    public MyChannelInboundCycleHandlerAdapter(CountDownLatch AWAIT_SIGNAL, AtomicBoolean SUCCESS) {
        this.AWAIT_SIGNAL = AWAIT_SIGNAL;
        this.SUCCESS = SUCCESS;
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) {
        // 连接建立成功，发送请求
        Scanner scanner = new Scanner(System.in);
        // 需要新建线程执行，最好别使用NIO线程
        Thread inputThread = new Thread(() -> {
            System.out.println("请输入用户名: ");
            String username = scanner.nextLine();
            System.out.println("请输入密码: ");
            String password = scanner.nextLine();
            LoginRequestMessage loginRequestMessage = new LoginRequestMessage(username, password);
            ctx.writeAndFlush(loginRequestMessage);
            try {
                AWAIT_SIGNAL.await();
                if (!SUCCESS.get()) {
                    // 登录错误，关闭客户端
                    ctx.channel().close();
                    return;
                }
                while (true) {
                    System.out.println("==================================");
                    System.out.println("send [username] [content]");
                    System.out.println("gsend [group name] [content]");
                    System.out.println("gcreate [group name] [m1,m2,m3...]");
                    System.out.println("gmembers [group name]");
                    System.out.println("glist [group name]");
                    System.out.println("gjoin [group name]");
                    System.out.println("gquit [group name]");
                    System.out.println("quit");
                    System.out.println("==================================");
                    String line = scanner.nextLine();
                    String[] info = line.split(" ");
                    switch (info[0]) {
                        case "send":
                            ChartRequestMessage chartRequestMessage = new ChartRequestMessage(info[1], info[2]);
                            ctx.writeAndFlush(chartRequestMessage);
                            break;
                        case "gsend":
                            GroupChartRequestMessage groupChartRequestMessage = new GroupChartRequestMessage(info[1], info[2]);
                            ctx.writeAndFlush(groupChartRequestMessage);
                            break;
                        case "gcreate":
                            // 创建聊天组
                            CreateGroupRequestMessage createGroupRequestMessage = new CreateGroupRequestMessage(info[1]);
                            ctx.writeAndFlush(createGroupRequestMessage);
                            break;
                        case "gmembers":
                            // 列出某个聊天组的成员
                            ListGroupMembersRequestMessage listGroupMembersRequestMessage = new ListGroupMembersRequestMessage(info[1]);
                            ctx.writeAndFlush(listGroupMembersRequestMessage);
                            break;
                        case "glist":
                            // 列出某个聊天组的成员
                            ListGroupsRequestMessage listGroupsRequestMessage = new ListGroupsRequestMessage();
                            ctx.writeAndFlush(listGroupsRequestMessage);
                            break;
                        case "gjoin":
                            JoinGroupRequestMessage joinGroupRequestMessage = new JoinGroupRequestMessage(info[1]);
                            ctx.writeAndFlush(joinGroupRequestMessage);
                            break;
                        case "gquit":
                            QuitGroupRequestMessage quitGroupRequestMessage = new QuitGroupRequestMessage(info[1]);
                            ctx.writeAndFlush(quitGroupRequestMessage);
                            break;
                        default:
                            ctx.channel().close();
                            log.debug("客户端即将关闭");
                            return;
                    }
                }
            } catch (InterruptedException interruptedException) {
                interruptedException.printStackTrace();
            }
        });

        // 设置守护线程启动, Nio线程结束，此线程随之销毁
        inputThread.setDaemon(true);

        inputThread.start();
    }
}
